package com.lczc.soa.controller;

import com.lczc.soa.domain.Apply;
import com.lczc.soa.domain.Door;
import com.lczc.soa.domain.SearchApplyResponse;
import com.lczc.soa.service.ApplyService;
import com.lczc.soa.service.DoorService;
import com.lczc.soa.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/Apply")
public class ApplyController {
    @Autowired
    private ApplyService applyService;

    @Autowired
    private DoorService doorService;

    @Autowired
    private UserService userService;

    @RequestMapping("/list")
    public String ApplyList(){
        return "apply_list";
    }

    @GetMapping("/api/search")
    @ResponseBody
    public SearchApplyResponse searchApplyResponse(@RequestParam("pageNum") Integer pageNum,
                                                   @RequestParam("pageSize") Integer pageSize){
        return applyService.findAllApply(pageNum,pageSize);
    }

    @GetMapping("/api/searchLike")
    @ResponseBody
    public SearchApplyResponse searchApplyResponseLike(@RequestParam("keywords") String keywords,
                                                     @RequestParam("pageNum") Integer pageNum,
                                                     @RequestParam("pageSize") Integer pageSize){
        return applyService.findApplyLikeName(keywords,pageNum,pageSize);
    }

    @RequestMapping("/pass")
    public String pass(@RequestParam("id") String id){
        Apply apply = applyService.findApplyById(id);
        apply.setState("审核通过");
        int updated = applyService.saveApply(apply);
        return "redirect:/Apply/list";
    }

    @RequestMapping("/reject")
    public String reject(@RequestParam("id") String id){
        Apply apply = applyService.findApplyById(id);
        apply.setState("未通过");
        int updated = applyService.saveApply(apply);
        Door door = doorService.findDoorByApply(apply.getDoorId());
        door.setUserId(null);
        doorService.saveDoor(door);
        return "redirect:/Apply/list";
    }

    @RequestMapping("/cancel")
    public String cancel(@RequestParam("id") String id){
        Apply apply = applyService.findApplyById(id);
        apply.setState("已取消");
        int updated = applyService.saveApply(apply);
        Door door = doorService.findDoorByApply(apply.getDoorId());
        door.setUserId(null);
        doorService.saveDoor(door);
        return "redirect:/User/toedit";
    }

    @RequestMapping("/reissue")
    public String reissue(@RequestParam("id") String id){
        Apply apply = applyService.findApplyById(id);
        Apply reapply = new Apply();
        reapply.setState("审核中");
        reapply.setDoorId(apply.getDoorId());
        reapply.setUserId(apply.getUserId());
        int updated = userService.saveApply(reapply);
        Door door = doorService.findDoorByApply(apply.getDoorId());
        door.setUserId(apply.getUserId());
        doorService.saveDoor(door);
        return "redirect:/User/toedit";
    }

}
